<?php
	
	require_once(ROOTDIR.'source/service/class/class.main.php');
	
	class _Fanli extends Main
	{
	
		private static $fanli = 'fanli';
		
		public static function chuliFanliInfo($where,$data,$record=array())
		{
			$link = self::getMysqlInstance();
			
			$link->query("SET AUTOCOMMIT=0");
			$link->query("BEGIN");
			$set = join(',',self::setMySQLParam($data));
			if(!empty($set) && !empty($where['id']))
			{
				$sql = sprintf("update %s set %s where id='%s'",self::$fanli,$set,$where['id']);
				fileLog($sql);
				$upres = $link->diy_update_sql($sql);
				
				if($upres)
				{
					$sql = sprintf("update users set users.account = (users.account + %s) where id='%s' ",$data['fanli'],$data['uid']);
					fileLog($sql);
					$upres2 = $link->diy_update_sql($sql);
					if($upres2)
					{
						
						if($record)
						{
							$sql = sprintf("update users set users.account = (users.account + %s) where id='%s' ",$record['fanli'],$record['uid']);
							fileLog($sql);
							$upres3 = $link->diy_update_sql($sql);
							
							if($upres3)
							{
								//追加返利记录
								$set = join(',',self::setMySQLParam($record));
								$sql = sprintf("insert into %s set %s ",self::$fanli,$set);
								$upres4 = $link->insert_sql($sql);

								if($upres4)
								{
									saveOpLogInfo('fanli',$upres4,'添加分享返利');
									$link->query("COMMIT");
								}
								else
								{
									$link->query("ROLLBACK");
								}
							}
							else
							{
								$link->query("ROLLBACK");
							}
						}
						else
						{
							$upres3  = true;
							$upres4 = true;
							$link->query("COMMIT");
						}
						
					}
					else
					{
						$link->query("ROLLBACK");
					}
				}
				else
				{
					$link->query("ROLLBACK");
				}
			}
			else
			{
				$link->query("ROLLBACK");
			}
			
			$link->query("END");
			$link->query("SET AUTOCOMMIT=1");
			
			if($upres && $upres2)
			{
				if($upres3 && $upres4)
				{
					return true;
				}
				else
				{
					return FALSE;
				}
			}
			else
			{
				return FALSE;
			}
		}
		
		public static function addFanliInfo($data)
		{
			$set = join(',',self::setMySQLParam($data));
			if(!empty($set))
			{
				$sql = sprintf("insert into %s set %s ",self::$fanli,$set);
				fileLog($sql);
				return self::getMysqlInstance()->insert_sql($sql);
			}
			else
			{
				return false;
			}
		}
		
		public static function setFanliInfo($where,$data)
		{
			$set = join(',',self::setMySQLParam($data));
			if(!empty($set) && !empty($where['id']))
			{
				$sql = sprintf("update %s set %s where id='%s'",self::$fanli,$set,$where['id']);
				fileLog($sql);
				return self::getMysqlInstance()->diy_update_sql($sql);
			}
			else
			{
				return false;
			}
		}
		
		public static function getFanliInfo($wheres)
		{
			$where = join(' and ',self::setMySQLParam($wheres));
			$sql = sprintf("select * from %s where %s",self::$fanli,$where);
			fileLog($sql);
			$res = self::getMysqlInstance()->query($sql);
			
			return $res[0];
		}
		
		public static function getFanliList($wheres,$page,$between,$sort)
		{
			$where = join(' and ',self::setMySQLParam($wheres));
			
			if(empty($where))
			{
				$where = ' 1=1 ';
			}
			
			if(isset($between['start']) && isset($between['end']))
			{
				$where .= ' and `addtime` between "'.$between['start'].'" and "'.$between['end'].'" ';
			}
			
			if(!empty($page))
			{
				$sqlCount = "select count(id) as count from fanli where ".$where;
			
				fileLog($sqlCount,'debug');
			
				$resCount = self::getMysqlInstance()->query($sqlCount);
				
				if(!empty($resCount[0]))
				{
					$pagenum=ceil($resCount[0]['count']/$page['rows']); 
				}
				else
				{
					$pagenum=ceil(0/$page['rows']); 
				}
				
				$offset=($page['page']-1)*$page['rows'];   
				$limit = " limit $offset,".$page['rows'];
			}
			$select = 'select * ';
			if(is_array($sort) && $sort)
			{
				$order =' order by ';
				foreach($sort as $k => $v)
				{
					$ordertemp[] =" `{$k}` ".self::getMysqlInstance()->strescape($v);
				}
				
				$order .= implode(',', $ordertemp);
			}
			$sql = sprintf("%s from %s where %s %s %s",$select,self::$fanli,$where,$order,$limit);
			fileLog($sql);
			if(!empty($page))
			{
				$result['list'] = self::getMysqlInstance()->query($sql);
				
				$result['pages'] = $pagenum;
			}
			else
			{
				$result =  self::getMysqlInstance()->query($sql);
			}
			return $result;
		}
		
	}